package service.user;

import java.sql.Connection;

import com.mysql.cj.util.StringUtils;

import dao.user.UserDao;
import dao.user.UserDaoImpl;
import entity.User;
import utils.SqlConnection;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 
 *
 */
public class UserServiceImpl implements UserService {
	Connection connection;
	UserDao userDao = new UserDaoImpl();
	User user = null;
	@Override
	public User login(String id, String pass) {
		// TODO Auto-generated method stub
		if(StringUtils.isNullOrEmpty(id)||StringUtils.isNullOrEmpty(pass))return null;
		user = new User();
		user.setId(id);
		user.setPassword(pass);
		
		connection = SqlConnection.getConnection();
		List<User> userList;
		try {
			userList = userDao.getUserList(connection, user);
			if(userList.size()>0) return userList.get(0);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("登录查询失败");
		}
		
		return null;
	}
	@Override
	public boolean addUser(User user) {
		// TODO Auto-generated method stub
		connection = SqlConnection.getConnection();
		int row = 0;
		try {
			row = userDao.addUser(connection, user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return row>0;
	}
	@Override
	public List<User> getUserList(User user) {
		// TODO Auto-generated method stub
		if(user==null)user = new User();
		connection = SqlConnection.getConnection();
		ArrayList<User> userList = new ArrayList<User>();
		try {
			userList = (ArrayList<User>) userDao.getUserList(connection, user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userList;
		
	}
	@Override
	public boolean upUser(User user) {
		// TODO Auto-generated method stub
		if(user==null)user = new User();
		connection = SqlConnection.getConnection();
		int row =0;
		try {
			row = userDao.upUser(connection, user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row>0;
	}
	@Override
	public boolean deleteUser(String id) {
		// TODO Auto-generated method stub
		connection = SqlConnection.getConnection();
		
		int row = 0;
		
		try {
			row = userDao.deleteUser(connection, id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return row > 0;
	}

}
